<?php


namespace App\Http\Middleware;


use Illuminate\Http\Request;

class ApiLog
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        //验证签名
        $uri = $request->path();
        $request_id = date('YmdHis') . rand(10000, 99999);
        $action = str_replace('/', '_', substr($uri, 4));
        $ip = $request->getClientIp();
        $data = json_encode($request->all());
//        dump($action, "[$request_id][{$ip}][{$request->getMethod()}][{$uri}]\r\n{$data}");
        \App\Utils\ApiLog::write($action, "[$request_id][{$ip}][{$request->getMethod()}][{$uri}]\r\n{$data}");
        $result = $next($request);
        $data = $result->getContent();

        \App\Utils\ApiLog::write($action, "[$request_id][callback]\r\n{$data}");
        return $result;
    }
}
